package com.csdj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.common.enums.AppHttpCodeEnum;
import com.csdj.common.utils.ResponseData;
import com.csdj.common.utils.ResponseResult;
import com.csdj.pojo.IvIninvoice;
import com.csdj.pojo.IvInvoice;
import com.csdj.service.IvIninvoiceService;
import com.csdj.service.IvInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: DengYuChang
 * @date: 2023-08-25 10:54
 * @description:
 */
@RestController
@RequestMapping("/ivIninvoice")
public class IvIninvoiceController {

    @Autowired
    private IvIninvoiceService ininvoiceService;

    @Autowired
    private IvInvoiceService invoiceService;

    @GetMapping("listmap")
    public ResponseData listMap(@RequestParam(defaultValue="1" ) int PageNum){
        Page<Map> page=new Page<>(PageNum,5);
        Page<Map> listMap = ininvoiceService.listMap(page);
        return ResponseData.ok().putDataValue("page",listMap);
    }

    @PostMapping("addivIninvoice")
    public ResponseResult add(@RequestBody IvIninvoice ininvoice){
        boolean save = ininvoiceService.save(ininvoice);
        int startNo=Integer.parseInt(ininvoice.getStartNo());
        int endNo=Integer.parseInt(ininvoice.getEndNo());
        List<IvInvoice> invoices=new ArrayList<>();
        for (int i = startNo; i <=endNo ; i++) {
            IvInvoice invoice=new IvInvoice();
            invoice.setInvoiceNo(i+"");
            invoice.setInvoiceType(ininvoice.getInvoiceType());
            invoice.setInID(ininvoice.getID());
            invoice.setUsed(0);
            invoice.setInvalid(0);
            invoice.setArchived(0);
            invoices.add(invoice);

        }
        boolean b = invoiceService.saveBatch(invoices);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
